package com.chrhc.projects.cpub.gen.model;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import com.chrhc.projects.cpub.gen.util.ColumnData;
import com.chrhc.projects.cpub.gen.xml.IbatisConfigColumn;

public class ModelGenerateCodeLogic
{
    
    /**
     * 处理model的import逻辑
     * 
     * @param content
     * @return
     */
    public static String ModeImport(String content)
    {
        StringBuffer data = new StringBuffer();
        data.append("\r\r").append("import java.util.Date;");
        data.append("\r\r").append("import java.io.Serializable;");
        if (content.indexOf("@Length") > 0)
        {
            data.append("\r").append("import org.hibernate.validator.constraints.Length;");
        }
        if (content.indexOf("@Size") > 0)
        {
            data.append("\r").append("import javax.validation.constraints.Size;");
        }
        if (content.indexOf("@NotNull") > 0)
        {
            data.append("\r").append("import javax.validation.constraints.NotNull;");
        }
        if (content.indexOf("@JsonFormat") > 0)
        {
            data.append("\r").append("import com.fasterxml.jackson.annotation.JsonFormat;");
        }
        return data.toString();
    }
    
    /**
     * sql类型传java类型
     * 
     * @param content
     * @return
     */
    public static String sqlType2Java(String content)
    {
        content = content.replaceAll("java.lang.String", "String");
        content = content.replaceAll("java.sql.Timestamp", "Date");
        content = content.replaceAll("java.util.Date", "Date");
        content = content.replaceAll("java.lang.Integer", "Integer");
        return content;
    }
    
    public static String getBeanFeilds(String tableName, Map<String, IbatisConfigColumn> colsMap,
        List<ColumnData> dataList)
            throws SQLException
    {
        // List<ColumnData> dataList = createBean.getColumnDatas(tableName);
        StringBuffer str = new StringBuffer();
        StringBuffer getset = new StringBuffer();
        for (ColumnData d : dataList)
        {
            String name = d.getColumnName();
            String type = d.getDataType();
            String comment = d.getColumnComment();
            String nullable = d.getNullable();
            String cMaxLen = d.getCharmaxLength();
            List<String> annos = null;
            IbatisConfigColumn column = (IbatisConfigColumn)colsMap.get(name);
            if (column != null)
            {
                // annos = (List)colMap.get("annos");
                annos = column.getAnnotations();
            }
            name = reName(name);// 把库的字段名称改成pojo的字段名称
            String maxChar = name.substring(0, 1).toUpperCase();
            
            // str.append("\r\r\t").append("//").append(cMaxLen + " ").append(nullable).append(";");
            boolean ifAnno = false;
            
            if (nullable.equals("N"))
            {
                str.append("\r\r\t").append("@NotNull");
                ifAnno = true;
            }
            if (annos != null)
            {
                for (String anno : annos)
                {
                    if (ifAnno)
                    {
                        str.append("\r\t");
                    }
                    else
                    {
                        str.append("\r\r\t");
                    }
                    str.append(anno);
                    ifAnno = true;
                }
            }
            
            if (cMaxLen != null && !cMaxLen.equals(""))
            {
                if (ifAnno)
                {
                    str.append("\r\t");
                }
                else
                {
                    str.append("\r\r\t");
                }
                str.append("@Size(max=").append(cMaxLen).append(")");
                ifAnno = true;
            }
            if (ifAnno)
            {
                str.append("\r\t");
            }
            else
            {
                str.append("\r\r\t");
            }
            str.append("private ").append(type + " ").append(name).append(";//   ").append(comment);
            String method = maxChar + name.substring(1, name.length());
            
            getset.append("\r\r\t").append("public ").append(type + " ").append("get" + method + "() {\r\t");
            getset.append("    return this.").append(name).append(";\r\t}");
            
            getset.append("\r\r\t").append("public void ").append("set" + method + "(" + type + " " + name + ") {\r\t");
            getset.append("    this." + name + " = ").append(name);
            if (type.equals("java.lang.String"))
            {
                getset.append(" == null ? null : ").append(name).append(".trim()");
            }
            
            getset.append(";\r\t}");
        }
        
        // return str.toString() + getset.toString();
        return sqlType2Java(str.toString() + getset.toString());
    }
    
    private static String reName(String name)
    {
        String arr[] = name.split("_");
        int i = 0;
        StringBuffer buf = new StringBuffer();
        for (String s : arr)
        {
            if (i > 0)
            {
                buf.append(s.substring(0, 1).toUpperCase() + s.substring(1));
            }
            else
            {
                buf.append(s);
            }
            i++;
        }
        return buf.toString();
    }
    
    public static void main(String[] args)
    {
        // TODO Auto-generated method stub
        
    }
    
}
